<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名字"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆编号" prop="vehicleCode">
        <el-input
          v-model="queryParams.vehicleCode"
          placeholder="请输入车辆编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button> 
         <el-button
          type="primary"
          @click="handleAdd"
          v-hasPermi="['manage:drivers:add']"
        >新增数据</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="driversList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
     
      <el-table-column label="名字" align="center" prop="name" />
      <el-table-column label="性别" align="left" width="60"prop="gender">
        <template #default="scope">
          <dict-tag :options="gender" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center" width="120"prop="phoneNumber" />
      <el-table-column label="驾驶证类型" align="center" prop="drivingLicense">
        <template #default="scope">
          <dict-tag :options="driving_license" :value="scope.row.drivingLicense"/>
        </template>
      </el-table-column>
      <el-table-column label="驾照分数" align="center" width="90"prop="licensePoints" />
      <el-table-column label="身份证" align="center" prop="idNumber" show-overflow-tooltip/>
      <el-table-column label="家庭住址" align="center" prop="homeAddress" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
  
          <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['manage:drivers:edit']">修改</el-button>
          <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['manage:drivers:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改驾驶员资料管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="driversRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入名字" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option
              v-for="dict in gender"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="驾驶证类型" prop="drivingLicense">
          <el-select v-model="form.drivingLicense" placeholder="请选择驾驶证类型">
            <el-option
              v-for="dict in driving_license"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆编号" prop="vehicleCode">
          <el-input v-model="form.vehicleCode" placeholder="请输入车辆编号" />
        </el-form-item>
        <el-form-item label="驾照分数" prop="licensePoints">
          <el-input v-model="form.licensePoints" placeholder="请输入驾照分数" />
        </el-form-item>
        <el-form-item label="身份证" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="homeAddress">
          <el-input v-model="form.homeAddress" placeholder="请输入家庭住址" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Drivers">
import { listDrivers, getDrivers, delDrivers, addDrivers, updateDrivers } from "@/api/manage/drivers";

const { proxy } = getCurrentInstance();
const { driving_license, gender } = proxy.useDict('driving_license', 'gender');

const driversList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    vehicleCode: null,
  },
  rules: {
    name: [
      { required: true, message: "名字不能为空", trigger: "blur" }
    ],
    gender: [
      { required: true, message: "性别不能为空", trigger: "change" }
    ],
    phoneNumber: [
      { required: true, message: "电话不能为空", trigger: "blur" }
    ],
    drivingLicense: [
      { required: true, message: "驾驶证类型不能为空", trigger: "change" }
    ],
    vehicleCode: [
      { required: true, message: "车辆编号不能为空", trigger: "blur" }
    ],
    licensePoints: [
      { required: true, message: "驾照分数不能为空", trigger: "blur" }
    ],
    idNumber: [
      { required: true, message: "身份证不能为空", trigger: "blur" }
    ],
    homeAddress: [
      { required: true, message: "家庭住址不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询驾驶员资料管理列表 */
function getList() {
  loading.value = true;
  listDrivers(queryParams.value).then(response => {
    driversList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    gender: null,
    phoneNumber: null,
    drivingLicense: null,
    vehicleCode: null,
    licensePoints: null,
    idNumber: null,
    homeAddress: null,
    createdAt: null,
    updatedAt: null,
    createdBy: null,
    updatedBy: null,
    remarks: null
  };
  proxy.resetForm("driversRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加驾驶员资料管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getDrivers(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改驾驶员资料管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["driversRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateDrivers(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDrivers(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除驾驶员资料管理编号为"' + _ids + '"的数据项？').then(function() {
    return delDrivers(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/drivers/export', {
    ...queryParams.value
  }, `drivers_${new Date().getTime()}.xlsx`)
}

getList();
</script>
